package cn.lanqiao.ui;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.lanqiao.entity.Users;
import cn.lanqiao.service.UserService;
import cn.lanqiao.service.impl.UserServiceImpl;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.util.UUID;
import java.awt.event.ActionEvent;

public class addUsersFrm extends JDialog {
	private static final long serialVersionUID = 6870696607797410254L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtAccount;
	private JTextField txtPassword;
	private JTextField txtTelephone;
	private JTextField txtRole;
	public static void main(String[] args) {
		try {
			addUsersFrm dialog = new addUsersFrm();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//构造函数传递数据
	ManUserFrm frm =null;
	public addUsersFrm(ManUserFrm frm) {
		this();
		this.frm = frm;
	}
	
	public addUsersFrm() {
		setTitle("添加账号");
		setBounds(100, 100, 469, 430);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel label = new JLabel("账号：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label.setBounds(109, 41, 72, 28);
		contentPanel.add(label);
		
		txtAccount = new JTextField();
		txtAccount.setBounds(196, 46, 105, 28);
		contentPanel.add(txtAccount);
		txtAccount.setColumns(10);
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_1.setBounds(109, 90, 72, 24);
		contentPanel.add(label_1);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(196, 93, 105, 24);
		contentPanel.add(txtPassword);
		txtPassword.setColumns(10);
		
		JLabel label_2 = new JLabel("电话号码:");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_2.setBounds(109, 140, 88, 24);
		contentPanel.add(label_2);
		
		txtTelephone = new JTextField();
		txtTelephone.setBounds(196, 143, 105, 24);
		contentPanel.add(txtTelephone);
		txtTelephone.setColumns(10);
		
		JLabel label_3 = new JLabel("身份:");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_3.setBounds(109, 191, 72, 24);
		contentPanel.add(label_3);
		
		txtRole = new JTextField();
		txtRole.setBounds(196, 194, 105, 24);
		contentPanel.add(txtRole);
		txtRole.setColumns(10);
		
		JButton cancel = new JButton("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取消
				addUsersFrm.this.dispose();
			}
		});
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		cancel.setBounds(31, 308, 113, 27);
		contentPanel.add(cancel);
		
		JButton save = new JButton("确认添加");
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//确认修改
				String uuid =UUID.randomUUID().toString();
				//取文本输入框里的数据
				String account = txtAccount.getText();
				String password = txtPassword.getText();
				String telephone = txtTelephone.getText();
				String role = txtRole.getText();
				//封装到对象
				Users user = new Users(uuid,account,password,telephone,role);
				//调业务层
				UserService addUsers = new UserServiceImpl();
			 	addUsers.addUser(user);
			 	//关闭次窗体
			 	addUsersFrm.this.dispose();
			 	//刷新数据
			 	frm.bindJTable();
			}
		});
		save.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		save.setBounds(279, 311, 125, 27);
		contentPanel.add(save);
	}
}
